package com.eveinfo.api.accessors;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.eveinfo.EveInfo;
import com.eveinfo.api.ApiAuthorization;
import com.eveinfo.api.ApiException;
import com.eveinfo.api.NoCacheFoundExecption;
import com.eveinfo.api.character.list.ApiCharacter;
import com.eveinfo.api.character.list.CharacterListParser;
import com.eveinfo.api.character.list.CharacterListResponse;
import com.eveinfo.tools.EveInfoPreferences;
import com.eveinfo.tools.Tools;

public class CharacterList {
	private ArrayList<ApiCharacter> characterList;
	
	public CharacterList(int userID, long characterID, String apiKey)
			throws IOException, SAXException, ParserConfigurationException, ApiException, NoCacheFoundExecption {
		CharacterListParser parser = CharacterListParser.getInstance();
		final EveInfoPreferences preferences = EveInfo.getPreferences();
		if (preferences.isCacheOnly()) {
			parser.setCacheOnly(true);
			preferences.setEnableCache(true);
		} else {
			parser.setCacheOnly(false);
		}
		if (preferences.isEnableCache()) {
			Tools.setCacheEnable(parser);
		} else {
			Tools.setCacheDisable(parser);
		}
		CharacterListResponse response = parser.getEveCharacters(new ApiAuthorization(userID,
				characterID, apiKey));
		characterList = new ArrayList<ApiCharacter>(response.getEveCharacters());
	}

	public ArrayList<ApiCharacter> getCharacterList() {
		return characterList;
	}
	
	public List<String> getCharacterNameList(){
		List<String> list = new ArrayList<String>();
		for (ApiCharacter character : characterList) {
			list.add(character.getName());
		}
		return list;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		for (ApiCharacter character : characterList) {
			sb.append(character.getName());
			sb.append(" ");
		}
		return sb.toString();
	}
}
